import 'dart:convert';

import 'package:base_network/export.dart';
import 'package:base_widgets/export.dart';
import 'package:flutter/material.dart';

import '../common/common_util.dart';
import '../constant/app_router.dart';
import '../pages/login/login_change_notifier.dart';
import '../pages/login/page_login.dart';

/// author:liuhaichao
/// description:检查状态码来判断请求是否成功
/// create date：2020-11-09 on 8:48 PM
class LoginCheckInterceptor extends Interceptor {
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    String dataString = response.data.toString();
    dynamic responseJson = json.decode(dataString);
    if (responseJson is Map) {
      Map<String, dynamic> jsonObject = json.decode(dataString);
      if (jsonObject.containsKey('errorCode')) {
        //-1001：需要登录
        if (jsonObject['errorCode'] == -1001) {
          CommonUtil.jumpToPageByNavigatorState(AppRouter.login);
        }
      }
    }
    super.onResponse(response, handler);
  }
}
